<?php
// Loop functions

/**
 * framework_entry_header - adds post metadata before the entry_content div
 *
 * @since 0.3
 */
function framework_entry_header( $postmeta = '%date% %author% %comments% %edit%' ) {
	do_action( 'framework_hook_loop_open' ); // Available hook: framework_hook_loop_open

	if ( is_singular() ) {
			$entry_title = '<h1 class="entry-title">' . get_the_title() . "</h1>\n";
	} else {
			$entry_title  = '<h2 class="entry-title"><a href="';
			$entry_title .= get_permalink();
			$entry_title .= '" title="';
			$entry_title .= 'Permalink to ' . the_title_attribute( 'echo=0' );
			$entry_title .= '" rel="bookmark">';
			$entry_title .= get_the_title();
			$entry_title .= "</a></h2>\n";
	}
	
	echo $entry_title;
	if ( is_page() || is_404() ) return;
	
	echo "\n<!--BEGIN .entry-meta .entry-header-->\n";
	echo "<div class=\"entry-meta entry-header\">\n";
	framework_postmeta( $postmeta );
	echo "\n<!--END .entry-meta .entry-header-->\n";
	echo "</div>\n";
}

/**
 * framework_entry_footer - adds post metadata after the entry_content div
 *
 * @since 0.3
 */
function framework_entry_footer( $postmeta = '%categories% %tags%' ) {
	if ( is_page() or is_404() or is_attachment() or !$postmeta ) return;
	echo "\n<!--BEGIN .entry-meta .entry-footer-->\n";
	echo "<div class=\"entry-meta entry-footer\">\n";
	framework_postmeta( $postmeta );
	echo "\n<!--END .entry-meta .entry-footer-->\n";
	echo "</div>\n";
	
	do_action( 'framework_hook_loop_close' ); // Available hook: framework_hook_loop_close
}

/**
 * framework_entry_content - dynamically display the entry_content div
 *
 * @since 0.3
 * @needsdoc
 * @filter framework_entry_content
 * @uses framework_hook_entry_content
 */
function framework_entry_content( $tag = 'div' ) {
	$tag = apply_filters( 'framework_entry_content_tag', (string) 'div' ); // Available filter: framework_entry_content_tag
	if ( is_archive() or is_search() ) { $type = 'summary'; } else { $type = 'content';  }
	
	$open  = "<!--BEGIN .entry-". $type ."-->\n";
	$open .= "<". $tag ." class=\"entry-". $type ." article\">\n";
	$close  = "<!--END .entry-". $type ."-->\n";
	$close .= "</". $tag .">\n";
	
	// get_the_content and do WP default stuff
	$get_the_content = get_the_content();
	$get_the_content = apply_filters( 'the_content', $get_the_content ); // Available Filter: the_content
	$get_the_content = str_replace(']]>', ']]&gt;', $get_the_content );
	
	// excerpt and do WP default stuff
	$get_the_excerpt = apply_filters( 'the_excerpt', get_the_excerpt() );

	// archive.php, author.php, category.php, tag.php, date.php, search.php
	if ( is_archive() or is_search() ) {
		$content = $get_the_excerpt; // create framework_hook_excerpt/framework_excerpt
	}

	// attachment.php
	elseif ( is_attachment() ) {
		$content  = framework_attachment();
		$content .= $get_the_content;
	}

	// page.php
	elseif ( is_page() ) {
		$content = $get_the_content;
	}

	// index.php, single.php
	elseif ( is_front_page() or is_single() ) {
		$content  = $get_the_content;
		$content .= wp_link_pages( array( 'before' => '<div class="page-links"><p><span class="pages">Pages:</span>', 'after' => '</p></div>', 'next_or_number' => 'number' ) );
	}

	// 404.php
	elseif ( is_404() ) {
		$content  = "<p>Sorry, but you are looking for something that isn't here.</p>\n";
		$content .= get_search_form();
	}
	else {
		$content  = $get_the_content;
		$content .= wp_link_pages( array( 'before' => '<div class="page-links"><p><span class="pages">Pages:</span>', 'after' => '</p></div>', 'next_or_number' => 'number' ) );
	}
	
	$the_content = $open . $content . $close;
	echo apply_filters( 'framework_entry_content', (string) $the_content ); // Available filter: framework_entry_content
}

/**
 * framework_postmeta() - post meta
 *
 * @since 0.3
 * @credits k2
 */
function framework_postmeta( $meta_format = NULL ) {
	if ( ! $meta_format ) return;
	
	// No keywords to replace
	if ( strpos( $meta_format, '%' ) === false ) {
		echo $meta_format;
	} else {

		// separate the %keywords%
		$meta_array = preg_split('/(%.+?%)/', $meta_format, -1, PREG_SPLIT_DELIM_CAPTURE);

		// parse through the keywords
		foreach ($meta_array as $key => $str) {
			switch ($str) {
				case '%author%':
					$meta_array[$key] = framework_entry_author();
					break;

				case '%categories%':
					$meta_array[$key] = framework_entry_categories();
					break;

				case '%comments%':
					$meta_array[$key] = framework_entry_comments();
					break;

				case '%date%':
					$meta_array[$key] = framework_entry_date();
					break;

				case '%time%':
					$meta_array[$key] = framework_entry_time();
					break;

				case '%tags%':
					$meta_array[$key] = framework_entry_tags();
					break;
				
				case '%edit%':
					$meta_array[$key] = framework_entry_edit();
					break;
			}
		}
		// output the result
		echo implode( '', $meta_array );
	}
}

/**
 * framework_attachment - Handles a variety of attachments-types
 *
 * Figures out what type of attachment were dealing with
 * so we can output it correctly.
 *
 * @since 0.3
 * @needsdoc
 * @filter framework_attachment_class, framework_attachment_width, framework_attachment_height, framework_attachment_image, framework_attachment_video, framework_attachment_text, framework_attachment_audio, framework_attachment
 * @hook framework_hook_attachment, framework_hook_attachment_image, framework_hook_attachment_video, framework_hook_attachment_text, framework_hook_attachment_audio, framework_hook_attachment_application
 */
function framework_attachment() {
	do_action( 'framework_hook_attachment' ); // Available hook: framework_hook_attachment
	global $post;
	
	$tag = apply_filters( 'framework_attachment_tag', (string) 'div' ); // Available filter: framework_attachment_tag
	$open  = "<!--BEGIN .entry-attachment-->\n";
	$open .= "<". $tag ." class=\"entry-attachment\">\n";
	$close  = "<!--END .entry-attachment-->\n";
	$close .= "</". $tag .">\n";

	$attachment_class = apply_filters( 'framework_hook_attachment_class', (string) str_replace( '/', '-', get_post_mime_type() ) ); // Available filter: framework_hook_attachment_class
	
	// Basic image handling
	$attachment_image  = "<a href=\"". wp_get_attachment_url() ."\" title=\"". get_the_title() ."\" rel=\"attachment\">\n";
	$attachment_image .= "<img class=\"$attachment_class\" width=\"$width\" height=\"$height\" src=\"";
	$attachment_image .= wp_get_attachment_url() ."\" alt=\"". get_the_title() ."\" title=\"". get_the_title() ."\"";
	$attachment_image .= " />\n</a>\n";
	
	// Basic attachment handling
	$attachment_default  = "<span class=\"$attachment_class\">Download <a href=\"". wp_get_attachment_url() ."\" title=\"Download ". get_the_title() ."\" rel=\"attachment\">";
	$attachment_default .= get_the_title();
	$attachment_default .= "</a></span>\n";
	
	// Get the medium size width/height from wp settings
	$width = apply_filters( 'framework_attachment_width', get_option( 'medium_size_w' ) ); // Available filter: framework_attachment_width
	$height = apply_filters( 'framework_attachment_height', get_option( 'medium_size_h' ) ); // Available filter: framework_attachment_height
	var_dump($height);
	// if attachment is an image
	if ( preg_match( '/image/i', get_post_mime_type() ) ) {
		do_action( 'framework_hook_attachment_image' ); // Available hook: framework_hook_attachment_image
		$attachment = apply_filters( 'framework_attachment_image', (string) $attachment_image ); // Available filter: framework_attachment_image
	}
	
	// if attachment is video
	elseif ( preg_match( '/video/i', get_post_mime_type() ) ) {
		do_action( 'framework_hook_attachment_video' ); // Available hook: framework_hook_attachment_video
		$attachment = apply_filters( 'framework_attachment_video', (string) $attachment_default ); // Available filter: framework_attachment_video
	}
	
	// if attachment is text
	elseif ( preg_match( '/text/i', get_post_mime_type() ) ) {
		do_action( 'framework_hook_attachment_text' ); // Available hook: framework_hook_attachment_text
		$attachment = apply_filters( 'framework_attachment_text', (string) $attachment_default ); // Available filter: framework_attachment_text
	}
	
	// if attachment is audio
	elseif ( preg_match( '/audio/i', get_post_mime_type() ) ) {
		do_action( 'framework_hook_attachment_audio' ); // Available hook: framework_hook_attachment_audio
		$attachment = apply_filters( 'framework_attachment_audio', (string) $attachment_default ); // Available filter: framework_attachment_audio
	}
	
	// if attachment is an application
	elseif ( preg_match( '/application/i', get_post_mime_type() ) ) {
		do_action( 'framework_hook_attachment_application' ); // Available hook: framework_hook_attachment_application
		$attachment = apply_filters( 'framework_attachment_application', (string) $attachment_default ); // Available filter: framework_attachment_application
	}
	
	$framework_attachment = $open . $attachment . $close;
	return apply_filters( 'framework_attachment', (string) $framework_attachment ); // Available filter: framework_attachment
}

/**
 * framework_not_found - displays error message when no post were found
 *
 * @since 0.3
 * @needsdoc
 * @todo add is_* conditionals so user gets more specific not found error messages.
 */
function framework_not_found() {
	?>
	<!--BEGIN #post-0-->
	<div id="post-0" class="<?php framework_entry_class(); ?>">
		<h1 class="entry-title">Not Found</h1>
		
		<!--BEGIN .entry-content-->
		<div class="entry-content article">
			<p>Sorry, but you are looking for something that isn't here.</p>
			<?php get_search_form(); ?>
		<!--END .entry-content-->
		</div>
	<!--END #post-0-->
	</div>
	<?php
}
?>